package cn.ecut.controller;


import cn.ecut.model.Loudong;
import cn.ecut.model.Message;
import cn.ecut.model.Shoufeibiaozhun;
import cn.ecut.model.Xiangmu;
import cn.ecut.service.LoudongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.*;

@Controller
@RequestMapping("/loudong")
public class LoudongController {

    @Autowired
    private LoudongService loudongService;

    @ResponseBody
    @RequestMapping("/show")
    public HashMap showallloudong(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        Integer count = loudongService.selectAllcount();
        List<Loudong> list = new ArrayList<>();
        list=loudongService.selectAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Loudong loudong){
        Message message = new Message();
        loudong.setLoudongbianma(loudongService.getloudongBianma());
        loudong.setQuyu(loudongService.selectQuyuByXiangmubianma(loudong.getXiangmubianma()));
        loudong.setShiyongzhuangtai(1);
        int n = loudongService.insert(loudong);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String loudongbianma){
        Message message = new Message();
        int n = loudongService.deleteByPrimaryKey(loudongbianma);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Loudong loudong){
        Message message =new Message();
        if(loudong.getQiyongzhuangtai()==0){
            loudong.setQiyongzhuangtai(1);
        }else {
            loudong.setQiyongzhuangtai(0);
        }
        int n = loudongService.updateQiyongzhuangtai(loudong);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap<String, Object> select(String searchResult){
        System.out.println("进入到了 /select");
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Loudong> list = new ArrayList<>();
        list = loudongService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Message update(Loudong loudong){
        Message message = new Message();
        int n = loudongService.updateByPrimaryKey(loudong);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }
}
